import { _decorator, Component, EventTouch, game, Game, instantiate, Label, macro, Node, Prefab, ProgressBarComponent, Sprite, SpriteFrame, Tween, tween, v3, Vec3 } from 'cc';
import { BattleEvent, C2SEvent, GameEvent, PropUpType, WssEvent } from './EventName';
import { CommAnim } from './commonComp/CommAnim';
import { HttpCom } from './net/HttpCom';
import { Config } from './Config';
import { GlobalVar } from './GlobalVar';
import { BossInfo } from './commonComp/InterfaceCom';
import { ModelWeapon } from './weaponfactory/ModelWeapon';
const { ccclass, property } = _decorator;

@ccclass('Battle')
export class Battle extends Component {

    @property(Node)
    bullet: Node = null;
    @property(Node)
    bulletContent: Node = null;
    @property(SpriteFrame)
    bulletSprite: SpriteFrame[] = [];

    @property(Node)
    boss: Node = null;
    @property(ProgressBarComponent)
    bossHpBar: ProgressBarComponent = null;
    @property(Label)
    bossHpLab: Label = null;

    @property(Node)
    playerQuan: Node = null

    @property(Node)
    airDropContent: Node = null;
    @property(Prefab)
    airDrop: Prefab = null;
    @property(Node)
    speedProp: Node = null;

    @property(Node)
    rewardProp: Node = null;


    @property(Label)
    userNameLab: Label = null;
    @property(Label)
    userIdLab: Label = null;

    @property(Label)
    coinLab: Label = null;
    @property(Label)
    diamondLab: Label = null;



    fireRate: number = 0.2
    bossPos: Vec3 = null

    onSpeed5BtnClick() {
        console.log("购买5分钟加速");
    }

    onSpeed60BtnClick() {
        console.log("购买60分钟加速");
    }

    protected onLoad(): void {
        this.bossPos = v3(this.boss.position.x, this.boss.position.y)
        //用户信息更新
        game.on(GameEvent.USER_INFO_UPDATE, this.updateView, this);
        //boss信息更新
        game.on(WssEvent.ON_BOSSINFO_UPDATE, this.ON_BOSSINFO_UPDATE, this);
        //boss掉落
        game.on(WssEvent.ON_BOSS_DROP, this.ON_BOSS_DROP, this);

        //出现空投
        game.on(WssEvent.ON_AIRDROP_APPEAR, this.ON_AIRDROP_APPEAR, this);

        //开火控制
        game.on(GameEvent.FIRE_CTRL, this.FIRE_CTRL, this);
        ModelWeapon.getInstance().getweaponLevel();
    }

    protected onDestroy(): void {
        game.off(GameEvent.USER_INFO_UPDATE, this.updateView, this);
    }

    ON_BOSSINFO_UPDATE(bossInfo?: BossInfo) {
        this.bossHpBar.progress = bossInfo.body.lifebar / bossInfo.body.initLifebar
        let percent = (bossInfo.body.lifebar / bossInfo.body.initLifebar * 100).toFixed(0) + "%"
        this.bossHpLab.string = percent;
    }

    ON_BOSS_DROP(data?: any) {
        game.emit(GameEvent.SHOW_REWARD_PROPUP, data, PropUpType.BossReward)

        this.bossHpBar.progress = 1

        this.bossHpLab.string = "100.00%";
    }



    ON_AIRDROP_APPEAR() {
        this.createAirDrop()
    }

    FIRE_CTRL(isFire?: boolean) {
        if (isFire) {
            this.startFire();
        } else {
            this.stopFire();
        }
    }

    start() {
        this.initView();
        this.schedule(() => {

        }, 2, macro.REPEAT_FOREVER, 0.1)
        this.startFire();
        this.bossPos = v3(this.boss.position.x, this.boss.position.y)

        //this.schedule(this.createAirDrop,5,111,1)
    }

    initView() {
        this.updateView();
    }

    updateView() {
        this.userIdLab.string = GlobalVar.userInfo.id;
        this.userNameLab.string = GlobalVar.userInfo.username;
        this.diamondLab.string = "0";
        GlobalVar.userInfo.walletList.forEach((e, i) => {

            if (e.coin) {

                if (e.coin.toLowerCase() == "gold") {
                    this.coinLab.string = e.normalBalance.toString();
                }
                if (e.coin == "DIAMOND_ORE") {
                    this.diamondLab.string = e.normalBalance.toString();
                }

            }
        })

    }

    startFire() {
        this.schedule(this.fireOnce, this.fireRate, macro.REPEAT_FOREVER, 1)
    }

    stopFire() {
        this.unschedule(this.fireOnce)
    }

    createAirDrop() {

        let drop = instantiate(this.airDrop)
        this.airDropContent.addChild(drop)
        let x = CommAnim.getRandomIntInRange(-300, 300)

        drop.setPosition(x, 1000)
        tween(drop)
            .by(15, { position: v3(0, -900, 0) })
            .start()
        let angle = Math.PI * 5 / 180;
        let speed = 5
        angle = 15
        let tween1 = tween(drop)
            .to(0.25 * speed, { angle: -angle })
            .to(0.25 * speed, { angle: 0 })
            .to(0.25 * speed, { angle: angle })
            .to(0.25 * speed, { angle: 0 })
            .union()
            .repeat(3)
            .start()
        //tween().repeatForever(tween1)
    }

    onAirDropClick(event: EventTouch) {
        console.log(event)

    }

    onSpeedBtnClick() {
        this.speedProp.active = true;
    }


    bossHitAnim: Tween<any> = null
    bossHitedAnim() {

        this.bossHitAnim && this.bossHitAnim.stop()
        this.boss.scale = v3(0.8, 0.8)
        this.boss.setPosition(this.bossPos)
        this.bossHitAnim = this.shakeAnimation(this.boss)
    }

    shakeAnimation(target: Node) {
        const duration = 0.1; // 动画持续时间
        const intensity = 10;  // 晃动的强度
        let scale1 = v3(0.1, 0.1, 0.1)
        let scale2 = v3(-0.1, -0.1, 0.1)
        // 创建 tween
        return tween(target)
            .by(duration / 6, { position: v3(0, intensity, 0), scale: ((scale1)) })  // 向右晃动
            .by(duration / 6, { position: v3(0, -intensity, 0), scale: scale2 })  // 向左晃动
            .by(duration / 6, { position: v3(0, intensity, 0), scale: (scale1) })  // 向右晃动
            .by(duration / 6, { position: v3(0, -intensity, 0), scale: scale2 })  // 向右晃动
            //.by(duration / 6, { position: target.position })  // 恢复原位
            .start();  // 启动动画
    }


    zoomNode(target: Node, duration: number, rate: number) {
        let scale1 = v3(0.1, 0.1, 0.1)
        let scale2 = v3(-0.1, -0.1, 0.1)
        tween(target)
            .by(duration, { scale: scale1 })
            .by(duration, { scale: scale2 })
            .by(duration, { scale: scale1 })
            .by(duration, { scale: scale2 })
            .start()
    }



    zoomNodeAnim: Tween<any> = null
    fireOnce() {
        if (GlobalVar.isWssReady) {
            let data = {
                cmd: 2
            }
            GlobalVar.wss.sendBinaryMessage(data)
            //game.emit(C2SEvent.ON_PLAYER_CLICK)
        }
        if (this.zoomNodeAnim) {
            this.zoomNodeAnim.stop()
            this.playerQuan.setScale(1, 1, 1)
        }
        this.zoomNodeAnim = CommAnim.zoomNode(this.playerQuan, 0.1, 0.1)

        let b = instantiate(this.bullet)
        let index = ModelWeapon.getInstance().level;
        if (ModelWeapon.getInstance().grade == "SSR") {
            index += 10;
        }
        index = Math.floor(index / 5);
        index = index < 0 ? 0 : index;
        index = index > 4 ? 4 : index;
        b.getComponent(Sprite).spriteFrame = this.bulletSprite[index];
        this.node.addChild(b)
        b.active = true
        b.setPosition(v3(26, -288, 0))
        let bossPos = 1
        tween(b)
            .to(0.33, { position: v3(12.5, this.boss.position.y, 0) })
            .call(() => {
                game.emit(BattleEvent.HIT_BOSS)
                this.bossHitedAnim()
                b.destroy()
            })
            .start();
    }

    update(deltaTime: number) {

    }
}


